//
//  ExploreSearchView.m
//  XPhoto
//
//  Created by baboy on 2019/8/30.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "ExploreSearchView.h"
#import <BKKit/BKTabBarView.h>
#import "Explore.h"
#import "UIScrollView+x.h"
#import "UserProfileViewController.h"
#import "UserProfileViewController.h"
#import "TopicPostViewController.h"
#import <BKKit/BKNavigationController.h>
#import "LocPostViewController.h"

@interface ExploreSearchView()<BKTabBarViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) NSMutableArray *items;
@end

@implementation ExploreSearchView
- (void)searchWithType:(NSString *)type keyword:(NSString *)keyword offset:(NSInteger)offset{
    ExploreSearchArgs *args = [ExploreSearchArgs new];
    args.type = type;
    args.keyword = keyword;
    
    [Explore searchWithArgs:args callback:^(id  _Nonnull task, NSArray *items, NSError * _Nonnull error) {
        if (error) {
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
        }
        if (!error && items) {
            if (offset == 0) {
                self.items = [NSMutableArray array];
            }
            [self.items addObjectsFromArray:items];
        }
        [self.tableView reloadData];
        [self.tableView stopLoading];
        self.tableView.loadMore = items.count == args.pageSize;
    }];
}
- (void)searchWithOffset:(NSInteger)offset{
    id item = [self.categoryView.items objectAtIndex:self.categoryView.selectedIndex];
    NSString *type = [item valueForKey:@"value"];
    [self searchWithType:type keyword:self.searchBar.text offset:offset];
}
#pragma mark - BKTabBarViewDelegate
- (void)tabBar:(BKTabBarView *)tabBar didSelectItem:(NSDictionary *)item{
    [self searchWithOffset:0];
}

#pragma mark - BKScrollViewDragRefreshDelegate
- (void)scrollViewWillBeginLoadingMore:(id)scrollView{
    [self searchWithOffset:self.items.count];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView scrollViewDidScroll];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.tableView scrollViewDidEndDragging];
}


#pragma mark - UISearchBar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    self.hidden = NO;
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    self.hidden = YES;
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self searchWithOffset:0];
    [searchBar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self searchWithOffset:0];
}

#pragma  mark - UITableView delegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger n = self.items.count;
    return n;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 64;
//}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExploreItem *item = [self.items objectAtIndex:indexPath.row];
    
    NSString *reuseCellId = @"TableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseCellId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = THEME_TEXT_COMMENT_FONT;
        cell.detailTextLabel.font = THEME_TEXT_FONT;
    }
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_USER]) {
        User *user = item.data;
        cell.textLabel.text = [NSString stringWithFormat:@"%@(@%@)", user.nickname, user.userName];;
    }
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_TOPIC]) {
        PostTopic *topic = item.data;
        cell.textLabel.text = [NSString stringWithFormat:@"#%@", topic.title];
    }
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_LOC]) {
        PostLoc *loc = item.data;
        cell.textLabel.text = loc.addr;
    }
//    cell.detailTextLabel.text = @"--";
    return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ExploreItem *item = [self.items objectAtIndex:indexPath.row];
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_USER]) {
        
        UserProfileViewController *vc = [[UserProfileViewController alloc] init];
        vc.user = item.data;
        [[self.delegate navigationController] pushViewController:vc animated:YES];
    }
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_TOPIC]) {
        PostTopic *topic = item.data;
        TopicPostViewController *vc = [[TopicPostViewController alloc] init];
        vc.topic = topic.title;
        [[self.delegate navigationController] pushViewController:vc animated:YES];
    }
    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_LOC]) {
        PostLoc *loc = item.data;
        LocPostViewController *vc = [[LocPostViewController alloc] init];
        vc.addr = loc.addr;
        vc.lat = loc.lat;
        vc.lng = loc.lng;
        [[self.delegate navigationController] pushViewController:vc animated:YES];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
